import {addYyDetailUrl, queryYyDetailUrl, modYyDetailUrl, delYyDetailUrl, queryAllYyDetailUrl} from '../services/operation';

export default {
    namespace:"adAddressEditAndAdd",

    state: {
        data: {
            list: [],
            pagination: {},
        },
        typeSList: [],
        loading: false,
    },

    effects: {
        // * fetch({payload}, {call, put}) {
        //     yield put({
        //         type: 'changeLoading',
        //         payload: true,
        //     });
        //     const response = yield call(queryYyDetailUrl, payload);
        //     yield put({
        //         type: 'save',
        //         payload: response,
        //     });
        //     yield put({
        //         type: 'changeLoading',
        //         payload: false,
        //     });
        // },
        * add({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(addYyDetailUrl, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        * mod({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(modYyDetailUrl, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        // * del({payload, callback}, {call, put}) {
        //     yield put({
        //         type: 'changeLoading',
        //         payload: true,
        //     });
        //     const response = yield call(delYyDetailUrl, payload);
        //     yield put({
        //         type: 'changeLoading',
        //         payload: false,
        //     });
        //
        //     if (callback) callback(response);
        // },

        * queryAllYyDetailUrl({payload, callback}, {call, put}){
            // yield put({
            //     type: 'changeLoading',
            //     payload: true,
            // });

            const response = yield call(queryAllYyDetailUrl, payload);
            // yield put({
            //    type: 'save',
            //    payload: response,
            //});
            //yield put({
            //    type: 'changeLoading',
            //    payload: false,
            //});
            if (callback) callback(response);
        },
    },

    reducers: {
        save(state, action) {
            const typeSList = [];
            getTypeSList(typeSList, action.payload.resultData.adAddressList.list);
            function getTypeSList(tmpR, list) {
                for (var i = 0; i < list.length; i++) {
                    const typeItem = {value: list[i]["detail_url"], label: list[i]["status"]};
                    if (list[i]["children"]) {
                        typeItem["children"] = [];
                        getTypeSList(typeItem["children"], list[i]["children"]);
                    }

                    tmpR.push(typeItem);
                }
            }
            return {
                ...state,
                typeSList: typeSList,
                data: action.payload.resultData.adAddressList,
            };
        },

        changeLoading(state, action) {
            return {
                ...state,
                loading: action.payload,
            };
        },
    },
};
